#include "cpp-httplib-0.7.15/httplib.h"
#include "jsoncpp/json/json.h"
#include "searcher.hpp"

const std::string raw_path = "data/raw_html/raw.txt"; // 数据源路径
const std::string Web_root_path = "./wwwroot";        // url中的web根目录

int main()
{
    // 构建索引
    Ns_Searcher::Searcher search;
    search.InitSearcher(raw_path);

    httplib::Server svr;                                                          // Http server object
    svr.set_base_dir(Web_root_path.c_str());                                      // 设置url定位资源的Web根目录
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &resp) // 使用http的GET方法 -- 从指定的"资源请求数据"
            {
        if (!req.has_param("word")) // 判断请求报文是否传参 -- http//abc.def/s?word=xxx
        {
            resp.set_content("You did not enter search keywords, please re-enter", "text/plain; charset=nft-8"); // 构建响应文本
            return;
        }
        std::string keyword = req.get_param_value("word"); // 获取请求传参的数据
        std::string josn_string;
        search.Search(keyword, &josn_string); // 搜索关键字，获取jsoncpp序列化后的字符串
        resp.set_content(josn_string, "application/json"); 
            });

    // 服务器开始监听
    svr.listen("0.0.0.0", 8080); // 端口号为8080
    return 0;
}